package com.itheima.Service.impl;

import com.itheima.Service.OrdersettingService;
import com.itheima.common.entity.OrderSettingDTO;
import com.itheima.common.entity.OrdersettingVO;
import com.itheima.common.utils.DateUtils;
import com.itheima.common.utils.POIUtils;
import com.itheima.dao.OrdersettingDao;
import com.itheima.pojo.OrderSetting;
import org.apache.poi.util.IOUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrdersettingServiceImpl implements OrdersettingService {
    @Autowired
    private OrdersettingDao ordersettingDao;

    /**
     * 模板文件下载
     *
     * @param response
     * @param
     * @return
     */
    @Override
    public void downloadFile(HttpServletResponse response, String filename) throws Exception {

        try {
            ServletOutputStream outputStream = response.getOutputStream();
            // 获取springboot resource 路径下的文件
            InputStream inputStream =
                    this.getClass().getClassLoader().getResourceAsStream("templates/ordersetting_template.xlsx");
//            XSSFWorkbook excel=new XSSFWorkbook(inputStream);
//            excel.write(outputStream);
            IOUtils.copy(inputStream, outputStream);  //将输入流写入输出流
            inputStream.close();
            outputStream.flush();
//            excel.close();
        } catch (Exception e) {
            throw new Exception("有问题，请联系后端");
        }


    }

    /**
     * 文件上传
     *
     * @param excelFile
     * @return
     * @throws Exception
     */
    @Override
    public void uploadFile(MultipartFile excelFile) throws Exception {
        List<String[]> readExcelList = POIUtils.readExcel(excelFile);  //读取上传的文件保存到list集合里
        System.out.println(readExcelList);
        for (String[] row : readExcelList) {    //遍历集合获取每行数据
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   //设置日期格式
            System.out.println("-----row0---------" + row[0] + "---r1--" + row[1]);  //看看日期是否有问题
            Date date = format.parse(row[0]);    //转成日期格式
            System.out.println("-----date---------" + date);
            OrderSetting orderSetting = new OrderSetting(date, Integer.parseInt(row[1]));  //封装到类里
            ordersettingDao.insert(orderSetting);  //添加到数据库中
        }
    }

    /**
     * 根据日期修改可预约人数
     *
     * @return
     */
    @Override
    public void editNumberByOrderDate(OrderSettingDTO orderSettingDTO) throws Exception {
        Date date = DateUtils.parseString2Date(orderSettingDTO.getOrderDate(), "yyyy-MM-dd");
        Integer number = Integer.parseInt(orderSettingDTO.getNumber());
        ordersettingDao.update(number, date);
    }

    /**
     * 根据年月查询对应的预约设置信息
     *
     * @param month
     * @return
     */
    @Override
    public List<OrdersettingVO> getOrderSettingByMonth(String month) throws Exception {
        String beginDateString = month + "-01";  //初始化month格式
        Date beginDate = DateUtils.parseString2Date(beginDateString, "yyyy-MM-dd"); //格式化month

        int monthLastDay = getMonthLastDay(beginDate); //获取该月最后一天
        String endDateString = month + "-" + monthLastDay;  //初始化该月最后一天格式
        Date endDate = DateUtils.parseString2Date(endDateString, "yyyy-MM-dd"); //格式化该月最后一天
        //传入该月第一天和最后一天，去数据库查找该月数据
        List<OrderSetting> orderSettingList = ordersettingDao.getOrderSettingByMonth(beginDate, endDate);

        List<OrdersettingVO> orderSettingVOList = new ArrayList<>();  //初始化返回结果的list类型
        for (OrderSetting orderSetting : orderSettingList) { //遍历查找出来的数据
            OrdersettingVO ordersettingVO = new OrdersettingVO(); //初始化类，并赋值
            ordersettingVO.setNumber(orderSetting.getNumber());
            ordersettingVO.setReservations(orderSetting.getReservations());
            ordersettingVO.setDate(orderSetting.getOrderDate().getDate()); //赋值 ：天
            orderSettingVOList.add(ordersettingVO); //加入集合
        }

        return orderSettingVOList;
    }

    /**
     * 获取Date类型月份的最后一天
     *
     * @param date
     * @return
     */
    public static int getMonthLastDay(Date date) {
        LocalDate localDate = LocalDate.of(date.getYear() + 1900, date.getMonth() + 1, 1);
        return localDate.lengthOfMonth();
    }

}
